<?php
/**
 * @package orinoco.framework
 * @subpackage framework
 * @author Ryan Yonzon <rawswift@gmail.com>
 * @copyright Copyright 2008 Ryan Yonzon 
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

require('controller.core.php');
// classes that will be available for application layer (developer's classes)  
require(CLASS_APPLICATION_DIR . 'controller.class.php');
require(DATABASE_DIR . 'record.class.php');

/**
 * framework's heart
 */
class coreConstructor extends coreController {

	protected $_application; 	// application
	protected $_controller; 	// controller
	protected $_method; 		// method
	protected $_id; 			// trailing ID
	protected $_get;			// trailing parameters
	protected $_post;			// post data

	/**
	 * constructor
	 *
	 * @param object $_route accepts applicationRoute object
	 * @return bool true if successful, otherwise false
	 */
	public function __construct($_route) {
		// do not continue if route is NULL/empty
		if(!isset($_route)) {
			throw new Exception('Cannot process empty route.');
		}
			$this->_application = $_route->_application; 
			$this->_controller = $_route->_controller;
			$this->_method = $_route->_method;
			
			if(isset($_route->_id)) {
				$this->_id = $_route->_id;
			}
			
			if(isset($_route->_get)) {
				$this->_get = $_route->_get;
			}

			if(isset($_route->_post)) {
				$this->_post = $_route->_post;
			}

		return true;
	}
	
	/**
	 * this is where the magic happens
	 */
	public function dispatch() {

		// check if controller file exists
		if($this->isControllerFileExists()) {
			// include controller file
			require($this->getControllerFilePath());

			// create an object of the developer's controller/class
			// using the class name as object name
			$_objname = $this->_controller . CONTROLLER_SUFFIX;
			$$_objname = new $_objname();
						
			// check if method exists
			if($this->isMethodExists()) {

				// execute method/action
				$_method_name = $this->_method;
				$$_objname->$_method_name();				

				// create inherited variables on-fly
				// variables that will be available for view templates
				foreach($$_objname as $k => $v) {
					$this->$k = $v;
				}		
								
				// construct view template
					// check if layout variable is set
					if(isset($this->__layout)) {
						$_layout_file = APPLICATION_DIR . $this->_application . '/' . LAYOUT_DIR . $this->__layout . LAYOUT_FILE_SUFFIX . LAYOUT_FILE_EXTENSION;	
							if (!file_exists($_layout_file)) {
								throw new Exception('Layout "' . $_layout_file . '" does not exists.');
							// load alternate layout
							} else {
								require($_layout_file);						
							}
					} else { // render default layout wrapper
						require($this->getDefaultLayout());
					}

			// no method found
			} else {
				throw new Exception('Method "' . $this->_method . '" does not exists.');
			}

		// no controller found
		} else {
			throw new Exception('Controller "' . $this->_controller . CONTROLLER_SUFFIX . '" does not exists.');
		}
		 
	} // end dispatch() method

	/**
	 * destructor
	 */
	public function __destruct() {
		unset($this);
	}
	
} // end class

// -EOF-